﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using StayInTouch.Core.Model;
using System.Xml.Serialization;


namespace StayInTouch.Core.Store
{
    public class FSStore : IContactStore 
    {
        private string fullFileName;
        public FSStore(string fullFileName)
        {
            this.fullFileName = fullFileName;
        }

        void IContactStore.SaveContacts(ContactList contactList)
        {
            FileStream fs = new FileStream(fullFileName, FileMode.OpenOrCreate);
            try
            {
                XmlSerializer serializer = new XmlSerializer(typeof(ContactList));
                serializer.Serialize(fs, contactList);
            }
            finally
            {
                fs.Close();
            }
        }

        ContactList IContactStore.LoadContacts()
        {
            FileStream fs = new FileStream(fullFileName, FileMode.Open);
            try
            {
                XmlSerializer serializer = new XmlSerializer(typeof(ContactList));
                ContactList res = (ContactList)serializer.Deserialize(fs);
                return res;
            }
            finally
            {
                fs.Close();
            }
        }
    }
}
